package kuaidi.view.fragment;


import android.os.Bundle;
import android.os.Environment;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.OptionalDataException;
import java.io.StreamCorruptedException;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import kuaidi.api.sqlite.RecordsDao;
import kuaidi.entity.kuaidijieguo.Kdjg;
import kuaidi.entity.kuaidijieguo.ListBean;
import kuaidi.entity.kuaidixianzhuang.Kuaidixianzhuang;
import kuaidi.helper.R;
import kuaidi.view.adapter.KdjgAdapter;

/**
 * A simple {@link Fragment} subclass.
 */
public class ShouyeFragment extends Fragment {


    @BindView(R.id.rlv_shouye)
    RecyclerView rlvShouye;

    public ShouyeFragment() {
        // Required empty public constructor
    }

    public static ShouyeFragment newInstance() {

        Bundle args = new Bundle();

        ShouyeFragment fragment = new ShouyeFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_shouye, container, false);
        ButterKnife.bind(this, view);
        initView();
        readno();
        return view;
    }
    private KdjgAdapter adapter;
    private void initView() {
        RecordsDao recordsDao=new RecordsDao(getContext());
        recordsDao.addRecords("1202343811417");
        GridLayoutManager manager=new GridLayoutManager(getContext(),1);
        adapter=new KdjgAdapter(getContext(),data);
        rlvShouye.setLayoutManager(manager);
        rlvShouye.setAdapter(adapter);
    }

    private List<ListBean> data = new ArrayList<>();
    private List<Kuaidixianzhuang> kdxzs=new ArrayList<>();
    private void readno() {
        Kdjg kdjg1 = null;
        List<String> recordsList = new ArrayList<>();
        RecordsDao recordsDao;
        recordsDao = new RecordsDao(getContext());
        recordsList = recordsDao.getRecordsList();
        for (int i = 0; i < recordsList.size(); i++) {
            kdjg1 = readKdjg(recordsList.get(i));
            String com=kdjg1.getResult().getCompany();
            String lastwuliu=kdjg1.getResult().getList().get(kdjg1.getResult().getList().size()-1).getRemark();
            String no=kdjg1.getResult().getNo();
            String status=kdjg1.getResult().getStatus();
            String time=kdjg1.getResult().getList().get(kdjg1.getResult().getList().size()-1).getDatetime();
            kdxzs.add(new Kuaidixianzhuang(com,lastwuliu,no,status,time));
            data.add(kdjg1.getResult().getList().get(kdjg1.getResult().getList().size()-1));
            adapter.notifyDataSetChanged();
        }
    }

    public Kdjg readKdjg(String no) {
        Kdjg kdjg1 = null;
        File sdCardDir = Environment.getExternalStorageDirectory();//获取SDCard目录
        File sdFile = new File(sdCardDir, no);

        try {
            FileInputStream fis = new FileInputStream(sdFile);   //获得输入流
            ObjectInputStream ois = new ObjectInputStream(fis);
            kdjg1 = (Kdjg) ois.readObject();
            ois.close();
        } catch (StreamCorruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (OptionalDataException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return kdjg1;
    }
}
